package com.youth.koodoReaderManager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youth.koodoReaderManager.entity.Book;
import com.youth.koodoReaderManager.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
@Api(tags = "书籍管理", description = "提供书籍的增删改查及分页查询接口")
public class BookController {

    private final BookService bookService;

    @Autowired
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @PostMapping
    @ApiOperation(value = "创建新书籍", notes = "添加新的书籍记录")
    public boolean createBook(@RequestBody Book book) {
        return bookService.save(book);
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新书籍信息", notes = "根据书籍ID更新书籍信息")
    public boolean updateBook(@PathVariable String id, @RequestBody Book book) {
        return bookService.updateById(book);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除书籍", notes = "根据书籍ID删除书籍记录")
    public boolean deleteBook(@PathVariable String id) {
        return bookService.removeById(id);
    }

    @GetMapping
    @ApiOperation(value = "分页查询书籍", notes = "根据查询条件分页获取书籍列表")
    public List<Book> pageQueryBooks(@RequestParam(defaultValue = "1") int current,
                                      @RequestParam(defaultValue = "10") int size,
                                      @RequestParam(required = false) String query) {
        // 创建分页对象
        Page<Book> page = new Page<>(current, size);
        // 调用服务层执行分页查询
        return bookService.pageQuery(page, query);
    }
}